package cn.hdufocus.focus_helper.service.impl;

import cn.hdufocus.focus_helper.bean.MailBean;
import cn.hdufocus.focus_helper.bean.RobotBean;
import cn.hdufocus.focus_helper.dao.ReservationDao;
import cn.hdufocus.focus_helper.entity.Reservation;
import cn.hdufocus.focus_helper.service.IReservationService;
import cn.hdufocus.focus_helper.util.CoolQUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

/**
 * @author YY-PC
 */
@Service
public class ReservationServiceImpl implements IReservationService {
    @Autowired
    ReservationDao reservationDao;
    @Autowired
    RobotBean robotBean;
    @Override
    public List<Reservation> listAll() {
        return reservationDao.findAll();
    }

    @Override
    public Reservation findById(long id) {
        return reservationDao.findById(id).get();
    }

    @Override
    public Reservation save(Reservation reservation) {
        reservation.setTimestamp(new Timestamp(System.currentTimeMillis()));
        reservation.setStatus(1);
        CoolQUtil.send(reservation,robotBean.getUrl(),robotBean.getDestination(),robotBean.getMessageType());
        return reservationDao.save(reservation);
    }

    @Override
    public boolean delete(long id) {
        Reservation reservation = reservationDao.findById(id).get();
        if (reservation!=null){
            reservationDao.delete(reservation);
            return true;
        }else {
            return false;
        }
    }

    @Override
    public boolean updateStatus(long id,int status) {
        Reservation reservation = reservationDao.findById(id).get();
        if (reservation!=null){
            reservation.setStatus(status);
            reservationDao.save(reservation);
            return true;
        }else {
            return false;
        }
    }

    @Override
    public boolean updateLinkman(long id,String linkman) {
        Reservation reservation = reservationDao.findById(id).get();
        if (reservation!=null){
            reservation.setLinkman(linkman);
            reservationDao.save(reservation);
            return true;
        }else {
            return false;
        }
    }

    @Override
    public List<Reservation> listByTime(Date fromTime, Date toTime) {
        return reservationDao.findAllByTimeBetween(fromTime,toTime);
    }

    @Override
    public List<Reservation> listByStatus(int status) {
        return reservationDao.findAllByStatus(status);
    }

    @Override
    public List<Reservation> listByPhone(String phone) {
        return reservationDao.findAllByPhone(phone);
    }

    @Override
    public List<Reservation> listByStatusBetween(int status1, int status2) {
        return reservationDao.findAllByStatusBetween(status1,status2);
    }
}
